804B - Minimum number of steps - CodeForces Solution


combinatorics greedy implementation math *1400

Please click on ads to support us..

Python Code:

def calcular_jogadas(entrada):
    MOD = 1000000007
    contagem_b = 0
    jogadas = 0
    indice = len(entrada) - 1

    while indice >= 0:
        letra = entrada[indice]
        if letra != 'b':
            jogadas = (jogadas + contagem_b) % MOD
            contagem_b = (2 * contagem_b) % MOD
        else:
            contagem_b = (contagem_b + 1) % MOD
        indice -= 1

    return jogadas

entrada = input()
resultado = calcular_jogadas(entrada)
print(resultado)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 1e9 + 7;
int n;
long long ans, cnt;
char a[N];
int main() {
	scanf("%s", a + 1);
	n = strlen(a + 1);
	for (int i = n; i >= 1; i--) {
		if (a[i] == 'a') {
			ans = (ans + cnt) % M;
			cnt = cnt * 2 % M;
		}
		else if (a[i] == 'b'){
			cnt++;
		}
//		cout << cnt << ' ' << ans << ' ' << i << endl;
	}
	printf("%lld", ans);
	return 0;
}
	   	 	 			 		   		  					   	


Comments

Submit
0 Comments
More Questions

1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space
1230B - Ania and Minimizing
1201A - Important Exam
676A - Nicholas and Permutation
431A - Black Square
474B - Worms
987B - High School Become Human
1223A - CME
1658B - Marin and Anti-coprime Permutation
14B - Young Photographer
143A - Help Vasilisa the Wise 2